﻿Imports System.Reflection
Imports System.Globalization
Imports System.Resources
Imports System.IO

Namespace BMLERP.Utils
	Public Class ResourceHelper
		Public Shared Function LoadCursor(ByVal assemblyType As Type, ByVal cursorName As String) As Cursor
			' Get the assembly that contains the bitmap resource
			Dim myAssembly As Assembly = Assembly.GetAssembly(assemblyType)

			' Get the resource stream containing the images
			Dim iconStream As Stream = myAssembly.GetManifestResourceStream(cursorName)

			' Load the Icon from the stream
			Return New Cursor(iconStream)
		End Function

		Public Shared Function LoadIcon(ByVal assemblyType As Type, ByVal iconName As String) As Icon
			' Get the assembly that contains the bitmap resource
			Dim myAssembly As Assembly = Assembly.GetAssembly(assemblyType)

			' Get the resource stream containing the images
			Dim iconStream As Stream = myAssembly.GetManifestResourceStream(iconName)

			' Load the Icon from the stream
			Return New Icon(iconStream)
		End Function

		Public Shared Function LoadIcon(ByVal _assembly As Assembly, ByVal iconName As String) As Icon
			' Get the resource stream containing the images
			Dim iconStream As Stream = _assembly.GetManifestResourceStream(iconName)

			' Load the Icon from the stream
			Return New Icon(iconStream)
		End Function

		Public Shared Function LoadIcon(ByVal assemblyType As Type, ByVal iconName As String, ByVal iconSize As Size) As Icon
			' Load the entire Icon requested (may include several different Icon sizes)
			Dim rawIcon As Icon = LoadIcon(assemblyType, iconName)

			' Create and return a new Icon that only contains the requested size
			Return New Icon(rawIcon, iconSize)
		End Function

		Public Shared Function LoadBitmap(ByVal _assembly As Assembly, ByVal _resourceName As String, ByVal _stringName As String, ByVal _culture As CultureInfo) As Bitmap
			Return LoadBitmap(_assembly, _resourceName, _stringName, _culture, False, New Point(0, 0))
		End Function

		Public Shared Function LoadBitmap(ByVal _assembly As Assembly, ByVal _resourceName As String, ByVal _stringName As String, ByVal _culture As CultureInfo, ByVal _makeTransparent As Boolean, ByVal _transparentPixel As Point) As Bitmap
			Dim rm As ResourceManager = New ResourceManager(_resourceName, _assembly)
			Dim image As Bitmap = Nothing

			Try
				image = rm.GetObject(_stringName, _culture)
			Catch mmrEx As MissingManifestResourceException
				MessageBox.Show("Missing " + _resourceName + ".resx resource file", "Resource not found", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Return image
			Catch ex As Exception
				MessageBox.Show(ex.ToString(), "Resource Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
				Return image
			End Try

			If (image Is Nothing) Then
				Return image
			End If

			If (_makeTransparent = True) Then
				image.MakeTransparent(image.GetPixel(_transparentPixel.X, _transparentPixel.Y))
			End If

			Return image
		End Function

		Public Shared Function LoadBitmap(ByVal imageName As String) As Bitmap
			Return LoadBitmap(Assembly.GetExecutingAssembly(), imageName, False, New Point(0, 0))
		End Function

		Public Shared Function LoadBitmap(ByVal assembly As Assembly, ByVal imageName As String) As Bitmap
			Return LoadBitmap(assembly, imageName, False, New Point(0, 0))
		End Function

		Public Shared Function LoadBitmap(ByVal assemblyType As Type, ByVal imageName As String) As Bitmap
			Return LoadBitmap(assemblyType, imageName, False, New Point(0, 0))
		End Function

		Public Shared Function LoadBitmap(ByVal assemblyType As Type, ByVal imageName As String, ByVal transparentPixel As Point) As Bitmap
			Return LoadBitmap(assemblyType, imageName, True, transparentPixel)
		End Function

		Public Shared Function LoadBitmap(ByVal assemblyType As Type, ByVal imageName As String, ByVal makeTransparent As Boolean, ByVal transparentPixel As Point) As Bitmap
			' Get the assembly that contains the bitmap resource
			Dim myAssembly As Assembly = Assembly.GetAssembly(assemblyType)
			Return LoadBitmap(myAssembly, imageName, makeTransparent, transparentPixel)
		End Function

		Public Shared Function LoadBitmap(ByVal assembly As Assembly, ByVal imageName As String, ByVal makeTransparent As Boolean, ByVal transparentPixel As Point) As Bitmap
			' Get the resource stream containing the images
			Dim image As Bitmap = Nothing
			Dim imageStream As Stream = assembly.GetManifestResourceStream(imageName)

			If (imageStream Is Nothing) Then
				Return image
			End If

			' Load the bitmap from stream
			image = New Bitmap(imageStream)

			If (makeTransparent = True) Then
				Dim backColor As Color = image.GetPixel(transparentPixel.X, transparentPixel.Y)
				' Make backColor transparent for Bitmap
				image.MakeTransparent(backColor)
			End If

			Return image
		End Function

		Public Shared Function LoadBitmapStrip(ByVal assemblyType As Type, ByVal imageName As String, ByVal imageSize As Size) As ImageList
			Return LoadBitmapStrip(assemblyType, imageName, imageSize, False, New Point(0, 0))
		End Function

		Public Shared Function LoadBitmapStrip(ByVal assemblyType As Type, ByVal imageName As String, ByVal imageSize As Size, ByVal transparentPixel As Point) As ImageList
			Return LoadBitmapStrip(assemblyType, imageName, imageSize, True, transparentPixel)
		End Function

		Public Shared Function LoadBitmapStrip(ByVal assembly As Assembly, ByVal imageName As String, ByVal imageSize As Size) As ImageList
			Return LoadBitmapStrip(assembly, imageName, imageSize, False, Point.Empty)
		End Function

		Public Shared Function LoadBitmapStrip(ByVal assemblyType As Type, ByVal imageName As String, ByVal imageSize As Size, ByVal makeTransparent As Boolean, ByVal transparentPixel As Point) As ImageList
			Return LoadBitmapStrip(Assembly.GetAssembly(assemblyType), imageName, imageSize, makeTransparent, transparentPixel)
		End Function

		Public Shared Function LoadBitmapStrip(ByVal assembly As Assembly, ByVal imageName As String, ByVal imageSize As Size, ByVal makeTransparent As Boolean, ByVal transparentPixel As Point) As ImageList
			' Create storage for bitmap strip
			Dim images As ImageList = New ImageList()

			' Define the size of images we supply
			images.ImageSize = imageSize

			' Get the assembly that contains the bitmap resource
			Dim myAssembly As Assembly = assembly

			' Get the resource stream containing the images
			Dim imageStream As Stream = myAssembly.GetManifestResourceStream(imageName)

			If (imageStream Is Nothing) Then
				Return images
			End If

			' Load the bitmap strip from resource
			Dim pics As Bitmap = New Bitmap(imageStream)

			If (makeTransparent = True) Then
				Dim backColor As Color = pics.GetPixel(transparentPixel.X, transparentPixel.Y)

				' Make backColor transparent for Bitmap
				pics.MakeTransparent(backColor)
			End If

			' Load them all
			images.Images.AddStrip(pics)

			Return images
		End Function

		Public Shared Function LoadString(ByVal _assembly As Assembly, ByVal _resourceName As String, ByVal _stringName As String) As String
			Return LoadString(_assembly, _resourceName, _stringName, CultureInfo.InvariantCulture)

		End Function

		Public Shared Function LoadString(ByVal _assembly As Assembly, ByVal _resourceName As String, ByVal _stringName As String, ByVal _culture As String) As String
			Return LoadString(_assembly, _resourceName, _stringName, CultureInfo.CreateSpecificCulture(_culture))
		End Function

		' NguyenVo: We mostly use this method to load string resource from Windows Form
		Public Shared Function LoadString(ByVal _assembly As Assembly, ByVal _resourceName As String, ByVal _stringName As String, ByVal _culture As CultureInfo) As String
			Dim rm As ResourceManager = New ResourceManager(_resourceName, _assembly)
			Return rm.GetString(_stringName, _culture)
		End Function
	End Class
End Namespace
